//
//  ZCScandetailsVC.m
//  zctx
//
//  Created by apple on 15/12/11.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCScandetailsVC.h"
#import "ZCSeeResultVC.h"
#import "ZCRaiseVC.h"
#import "ZCSearchModel.h"
#import "DZWebViewController.h"

#import "ZCSearchQualifiedCell.h"
#import "ZCSearchInvolvedCell.h"
#import "ZCSearchSamplingCell.h"
#import "ZCSearchCell.h"
#import "ZCSearchQualifiedOKCell.h"

#import "ZCSamplingDetailVC.h"
#import "ZCInvolvedDetailsVC.h"
#import "ZCProjectdetailsVC.h"
#import "ZCScanFootView.h"

@interface ZCScandetailsVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
//data
@property (nonatomic, strong) NSMutableArray *arrResult; //搜索结果数组
@property (nonatomic,assign)NSInteger offset; //页数


@end

@implementation ZCScandetailsVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ZCScandetailsVC"];
    [MTA trackPageViewBegin:@"ZCScandetailsVC"];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZCScandetailsVC"];
    [MTA trackPageViewEnd:@"ZCScandetailsVC"];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.offset = 0;
    self.view.backgroundColor = [UIColor colorWithHexString:@"#ebeded"];
//    NSLog(@"index = %@",self.barcodeDictionary);
    [self getData]; //self.arrResult 数据
    
    
    self.view.backgroundColor = [UIColor orangeColor];

    [self initTableView];
    //添加tableview
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCSearchQualifiedOKCell" bundle:nil] forCellReuseIdentifier:@"qualifiedOKcell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCSearchQualifiedCell" bundle:nil] forCellReuseIdentifier:@"SearchQualified"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCSearchInvolvedCell" bundle:nil] forCellReuseIdentifier:@"SearchInvolved"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCSearchSamplingCell" bundle:nil] forCellReuseIdentifier:@"SearchSampling"];
    
    
}

#pragma mark得到数据
-(void)getData{
    
    if ([self.barcodeDictionary[@"items"] isKindOfClass:[NSArray class]]) {
        NSArray *itemsArray =(NSArray *)self.barcodeDictionary[@"items"];
        self.arrResult = [itemsArray mutableCopy];
    }
}

#pragma mark 加载更多
-(void)loadMoreData{
    //上拉加载
    _offset = _offset+1;
    [self requestMainList];
}
#pragma mark - request 搜索请求

- (void)requestMainList {
    NSLog(@"上拉加载更多 = %@",self.barStr);

    [NetworkManager requestProductGetInfoByBarcodeWithMid:UserModel.mid barcode:self.barStr pagecount:[NSString stringWithFormat:@"%ld",(long)_offset] success:^(NSDictionary *dictResponse) {
        [self.tableView.mj_footer endRefreshing];
        
        NSObject *array = (NSObject *)[dictResponse objectForKey:@"content"];
        if ([array isKindOfClass:[NSDictionary class]]) {
            NSDictionary *dict =(NSDictionary *) array;
            if ([[dict objectForKey:@"items"] isKindOfClass:[NSArray class]]) {
                NSArray *itemsArray =(NSArray *)dict[@"items"];
                
                NSLog(@"111111111111 = %@",itemsArray);
                if (itemsArray !=nil && ![itemsArray isKindOfClass:[NSNull class]] && itemsArray.count !=0){
                    [self.arrResult addObjectsFromArray:itemsArray];
                    if (itemsArray.count>=10) {
                        if (!self.tableView.mj_footer) {
                            self.tableView.mj_footer = [MJDIYFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
                        }
                    }
                }
            else{
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
                [self.tableView reloadData];
        }
    }
    } failure:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
        NSLog(@"网络出错");

    }];
}

-(void)initTableView{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,SCREEN_HEIGHT -130-64) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    _tableView.sectionFooterHeight = 0.0001;
    _tableView.separatorColor = RGB(236, 237, 238);
    _tableView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
    [self.view addSubview:_tableView];

}


#pragma mark - UITableView DataSource & Delegate Methodsd

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.arrResult.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZCSearchModel *model;

    if (self.arrResult.count>0) {
        model = [ZCSearchModel modelObjectWithDictionary:self.arrResult[indexPath.row]];
    }
    if (model.isurl==1 ||model.result==1||model.result==2) {
        if (model.qualified == 1 || model.result ==1) {
            static NSString *cellIndentity = @"qualifiedOKcell";
            ZCSearchQualifiedOKCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentity];
            if (!cell) {
                cell = [[ZCSearchQualifiedOKCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentity];
            }
            cell.model = model;
            return cell;
        }else{
            static NSString *cellIndentity = @"SearchQualified";
            ZCSearchQualifiedCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentity];
            if (!cell) {
                cell = [[ZCSearchQualifiedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentity];
            }
            cell.searchModel = model;
            return cell;
        }
    }else{
        
        if (model.state==1) {
            static NSString *cellid =@"SearchInvolved";
            ZCSearchInvolvedCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
            if (cell == nil) {
                cell = [[ZCSearchInvolvedCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
            }
            cell.SearchModel = model;
            return cell;
        }
        else
        {// 0 审核中  2 抽样中 3完成 4 异常
            static NSString *cellid =@"SearchSampling";
            ZCSearchSamplingCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
            if (cell == nil) {
                cell = [[ZCSearchSamplingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
            }
            cell.samplingModel = model;
            return cell;
        }
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 92;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    ZCSearchModel* model = [ZCSearchModel modelObjectWithDictionary:self.arrResult[indexPath.row]];
    
    if (model.isurl ==1) {
            DZWebViewController *vc = [[DZWebViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
        
        NSLog(@"url = %@,name = %@",model.url,model.name);
            vc.strURL = model.url;
            vc.title = @"检测项目详情";
            [self.navigationController pushViewController:vc animated:YES];
    }else{
        ZCInvolvedDetailsVC *detailVC = [[ZCInvolvedDetailsVC alloc]init];
        detailVC.pid = model.internalZCSearchModelIdentifier;
        //    detailVC.stateInt = 3;
        detailVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (self.arrResult.count>0) {
        NSString *count = [NSString stringWithFormat:@"%@",self.barcodeDictionary[@"show_tips"]];
        if([count rangeOfString:@"\n"].location !=NSNotFound)//_roaldSearchText
        {
            return 60;
        }
        else
        {
            return 40;
        }
    }else{
        return 0.0001;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (self.arrResult.count>0) {
        
        NSString *count = [NSString stringWithFormat:@"%@",self.barcodeDictionary[@"show_tips"]];
        UIView *headView = [[UIView alloc]init];
        
        if([count rangeOfString:@"\n"].location !=NSNotFound)//_roaldSearchText
        {
            headView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 60);
        }
        else
        {
            headView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 40);
        }
        
        headView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
        headView.layer.borderColor =RGB(236, 237, 238).CGColor;
        headView.layer.borderWidth = 0.5;
        
        UILabel *PromptLable = [[UILabel alloc]init];
        [headView addSubview:PromptLable];
        PromptLable.numberOfLines = 0;
        [PromptLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(headView.centerY);
            make.height.equalTo(headView.height);
            make.left.equalTo(headView.left).with.offset(18);
            make.right.equalTo(headView.right).with.offset(-18);
        }];
        PromptLable.text =count;
        PromptLable.textColor = [UIColor colorWithHexString:@"#b2b2b2"];
        PromptLable.font = [UIFont systemFontOfSize:15];
        
        NSLog(@"head.title = %@",count);
        return headView;

    }else{
        UIView *footView = [[UIView alloc]init];
        footView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
        return footView;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    if (self.arrResult.count>0) {
        UIView *footView = [[UIView alloc]init];
        footView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
        return footView;
    }else{
        ZCScanFootView *footView = [[ZCScanFootView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
        return footView;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (self.arrResult.count>0) {
        return 0.001;
    }else{
        return 300;
    }
}

-(NSMutableArray *)arrResult{
    if (!_arrResult) {
        _arrResult = [NSMutableArray arrayWithCapacity:0];
    }
    return _arrResult;
}

@end
